import { REDIRECT } from '@/constants';
import qs from 'qs';
export default function withRedirect(targetLink) {
  const href = window.location.href;
  const localParams = qs.parse(href.split('?')[1]);
  const targetMatch = targetLink.split('?');
  const targetParams = qs.parse(targetMatch[1]);
  const pathName = targetMatch[0];
  if (localParams && localParams[REDIRECT]) {
    return `${pathName}?${qs.stringify({
      ...targetParams,
      [REDIRECT]: localParams[REDIRECT],
    })}`;
  }
  return targetLink;
}
